package com.camel.base.utils;

import com.alibaba.fastjson.JSONObject;
import com.camel.base.entity.MyConnection;
import com.camel.base.model.TbDatasource;
import com.camel.core.exceptions.CustomException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DBUtils {
    public static List<MyConnection> myConnections = new ArrayList<>();

    public static void main(String[] args) throws SQLException {
        TbDatasource tbDatasource = new TbDatasource();
        tbDatasource.setId(1);
        tbDatasource.setDbName("wm");
        tbDatasource.setDbAddr("192.168.11.4");
        tbDatasource.setDbPort("32432");
        tbDatasource.setDbType(2);
        tbDatasource.setLoginUsername("postgres");
        tbDatasource.setLoginPassword("1234");
        DBUtils dbUtils = new DBUtils();

        List<JSONObject> jsonObjects = dbUtils.getConnect(tbDatasource).excuteQuery("SELECT * FROM information_schema.schemata");
        System.out.println(jsonObjects);


    }

    public MyConnection connect(TbDatasource datasource) {
        String dbUrl = "";
        if (datasource.getDbType().equals(1)) {
            dbUrl = "jdbc:mysql://" + datasource.getDbAddr() + ":" + datasource.getDbPort() + "/" + datasource.getDbName() + "?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&allowMultiQueries=true&serverTimezone=GMT";
        } else if (datasource.getDbType().equals(2)) {
            dbUrl = "jdbc:postgresql://" + datasource.getDbAddr() + ":" + datasource.getDbPort() + "/" + datasource.getDbName() + "?modelcenter?allowMultiQueries=true";
        } else if (datasource.getDbType().equals(3)) {

        }
        // JDBC连接信息
        String dbUsername = datasource.getLoginUsername();
        String dbPassword = datasource.getLoginPassword();


        try {
            // 连接数据库
            Connection conn = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
            MyConnection myConnection = new MyConnection();
            myConnection.setConnection(conn);
            myConnection.setId(datasource.getId());

            boolean isAdd = false;
            for (int i = 0; i < myConnections.size(); i++) {
                MyConnection connection = myConnections.get(i);
                if(connection.getId().equals(datasource.getId())) {
                    myConnections.set(i, myConnection);
                    isAdd = true;
                }
            }
            if(!isAdd) {
                myConnections.add(myConnection);
            }
            myConnections.add(myConnection);

            return myConnection;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new CustomException("创建数据库连接失败");
        }
    }

    public MyConnection getConnect(TbDatasource datasource) throws SQLException {
        for (MyConnection myConnection : myConnections) {
            if(myConnection.getId().equals(datasource.getId())) {
                if(myConnection.connection.isValid(2000)) {
                    return myConnection;
                } else {
                    if(myConnection.connection.isValid(2000)) {
                        myConnection.connection.close();
                    }
                }
                return connect(datasource);
            }
        }
        return connect(datasource);

    }
}
